//
//  FourthTableViewController.m
//  Snigger
//
//  Created by 邓永强 on 16/8/15.
//  Copyright © 2016年 Smile. All rights reserved.
//

#import "FourthTableViewController.h"
#import "ViewModel.h"
#import "FourthTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import <MJRefresh.h>
#import <MJExtension.h>
#import "DetailsFourthViewController.h"
@interface FourthTableViewController ()
@property(nonatomic,strong)NSMutableArray *arrayData;

@property(nonatomic,strong)ViewModel *movieModel;
/** 上一次的请求参数 */
@property(nonatomic,strong)NSDictionary *params;

@property(nonatomic,strong)NSString *timestamp;

@end


static NSString *const MovieCellId = @"movie";



@implementation FourthTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"搞笑动漫";
    
    [self setViewAction];
    [self shuaxingAction];
    
}


-(void)setViewAction{
 
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FourthTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"model_cell"];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
}


-(void)shuaxingAction{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewMovie)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMovie)];
}


#pragma mark ------- 上拉刷新 -------------
- (void)loadNewMovie{
    
    // 结束下拉
    [self.tableView.mj_footer endRefreshing];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    self.params = params;
    
    [[AFHTTPSessionManager manager]GET:gaoxiaoImageUrl parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        if (self.params != params) return ;
        // 存储timestamp
        self.timestamp = responseObject[@"timestamp"];
        
        self.arrayData = [ViewModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"returnData"][@"comics"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            // 结束刷新
            [self.tableView.mj_header endRefreshing];
            
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.params != params) return ;
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
    }];
    
}

#pragma mark ------- 下拉加载 ----------
- (void)loadMoreMovie{
    
    // 结束上拉
    [self.tableView.mj_header endRefreshing];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"timestamp"] = self.timestamp;
    self.params = params;
    
    NSString *str = [MovieURL stringByAppendingFormat:@"%zd" , self.params];
    
    [[AFHTTPSessionManager manager]GET:str parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        if (self.params != params) return ;
        
        // 存储timestamp
        self.timestamp = responseObject[@"timestamp"];
        
        NSArray *newMovie = [ViewModel mj_objectArrayWithKeyValuesArray:responseObject[@"videos"]];
        [self.arrayData addObjectsFromArray:newMovie];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            // 结束刷新
            [self.tableView.mj_footer endRefreshing];
            
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.params != params) return ;
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        
    }];
}







-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    self.tableView.mj_footer.hidden = (self.arrayData.count == 0);
    return self.arrayData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FourthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"model_cell" forIndexPath:indexPath];
    
    cell.movie = self.arrayData[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    
    return 130;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







@end
